package com.example.wwk.graduationpractice.Adapter;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.example.wwk.graduationpractice.Activity.ChatConsultActivity;

import com.example.wwk.graduationpractice.Fragment.MainActivity;
import com.example.wwk.graduationpractice.NetDataUtils.LitePalUtils;
import com.example.wwk.graduationpractice.R;
import com.example.wwk.graduationpractice.UnreadBubble.BounceCircle;
import com.example.wwk.graduationpractice.UnreadBubble.RoundNumber;
import com.example.wwk.graduationpractice.UnreadBubbleUtils.PaintBounceUtil;
import com.example.wwk.graduationpractice.Bean.PsychologistBean;

import java.util.List;

/*
 * Created by wwk on 2017/3/2.
 */

public class PsychologyAdapter extends RecyclerView.Adapter<PsychologyAdapter.ViewHolder> {

    private List<PsychologistBean> mPsychologistList;
    private Context mcontext;
    private String thisuserName;
    private String user;
    BounceCircle mCircle;


    static class ViewHolder extends RecyclerView.ViewHolder {   //复用已有的View

        //    CardView cardView;
        View mView;
        TextView psychologistName;
        ImageView psychologistImage;
        TextView psychologistStatus;
        TextView psychologistLeaveWords;
        RoundNumber UnreadNumber;


        public ViewHolder(View itemView) {
            super(itemView);
//            cardView = (CardView) itemView;
            mView = itemView;
            psychologistName = (TextView) itemView.findViewById(R.id.psychologist_item_name);
            psychologistImage = (ImageView) itemView.findViewById(R.id.psychologist_item_image);
            psychologistStatus = (TextView) itemView.findViewById(R.id.psychologist_item_status);
            psychologistLeaveWords = (TextView) itemView.findViewById(R.id.psychologist_item_leaveWords);
            UnreadNumber = (RoundNumber) itemView.findViewById(R.id.psychologist_item_number);
        }
    }

    public PsychologyAdapter(List<PsychologistBean> psychologistList, String thisuser, BounceCircle circle) {
        mPsychologistList = psychologistList;           //传入数据list
        thisuserName = thisuser;
        mCircle = circle;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mcontext == null) {
            mcontext = parent.getContext();
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.psychologist_item, parent, false);

        final ViewHolder holder = new ViewHolder(view);
        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {//item点击事件
                int position = holder.getAdapterPosition();
                PsychologistBean psychologist = mPsychologistList.get(position);
                Intent intent = new Intent(mcontext, ChatConsultActivity.class);
                String userName =psychologist.getUser().substring(0,psychologist.getUser().indexOf("@"));//取jid的node
                intent.putExtra("nickName", psychologist.getName());         //获取该item的昵称
                intent.putExtra("userName",userName );       //该item代表的用户名
                intent.putExtra("thisuserName", thisuserName);

                if (psychologist.getUnreadCount() > 0) {  //如果有未读消息气泡显示，则在litepal中有相关数据
                    LitePalUtils.deleteData(thisuserName, user);  //删除litepal未读消息表中该cp的记录
                    psychologist.setUnreadCount(0);        //设置未读消息数量为0
                    PsychologistBean ps = psychologist;
                    mPsychologistList.remove(position);
                    mPsychologistList.add(position,ps);
                    PsychologyAdapter.this.notifyDataSetChanged();
                }
                mcontext.startActivity(intent);

            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        final PsychologistBean psychologist = mPsychologistList.get(position);        //获取当前位置

        if (psychologist.getBitmap() == null) {
            Glide.with(mcontext).load(psychologist.getImageId()).asBitmap().centerCrop().into(new BitmapImageViewTarget(holder.psychologistImage) {
                @Override//Glide加载圆形图片的完美方法
                protected void setResource(Bitmap resource) {
                    RoundedBitmapDrawable circularBitmapDrawable =
                            RoundedBitmapDrawableFactory.create(mcontext.getResources(), resource);
                    circularBitmapDrawable.setCircular(true);
                    holder.psychologistImage.setImageDrawable(circularBitmapDrawable);
                }
            });
        } else {
            holder.psychologistImage.setImageBitmap(psychologist.getBitmap());
        }


        holder.psychologistName.setText(psychologist.getName());
        holder.psychologistStatus.setText(psychologist.getStatus());
        int statusColor = psychologist.getStatusColor();
        if (statusColor == 0)             //根据用户状态改变状态text的颜色
            holder.psychologistStatus.setTextColor(Color.GRAY);
        else if (statusColor == 1)
            holder.psychologistStatus.setTextColor(Color.GREEN);
        holder.psychologistLeaveWords.setText(psychologist.getLeaveWords());
        //未读消息气泡
        int unreadcount = mPsychologistList.get(position).getUnreadCount();
        user = mPsychologistList.get(position).getUser().substring(0, mPsychologistList.get(position).getUser().indexOf("@"));
        if (unreadcount == 0)
            holder.UnreadNumber.setVisibility(View.INVISIBLE);
        else {
            holder.UnreadNumber.setVisibility(View.VISIBLE);    //显示气泡
            holder.UnreadNumber.setMessage("" + unreadcount);
        }


        mCircle.setFinishListener(new BounceCircle.FinishListener() {
            @Override
            public void onFinish() {
                LitePalUtils.deleteData(thisuserName, user);//删除litepal未读消息表中该cp的记录
                psychologist.setUnreadCount(0);        //设置未读消息数量为0
                PsychologistBean ps = psychologist;
                mPsychologistList.remove(position);
                mPsychologistList.add(position,ps);
                PsychologyAdapter.this.notifyDataSetChanged();
            }
        });
        holder.UnreadNumber.setClickListener(new RoundNumber.ClickListener() {
            @Override
            public void onDown() {
                int[] position = new int[2];
                holder.UnreadNumber.getLocationOnScreen(position);
                int radius = holder.UnreadNumber.getWidth() / 2;
                mCircle.down(radius, position[0] + radius, position[1] - 3*PaintBounceUtil.getTopBarHeight((MainActivity) mcontext) + radius, holder.UnreadNumber.getMessage());
                mCircle.setVisibility(View.VISIBLE); // 显示全屏范围的BounceCircle
                holder.UnreadNumber.setVisibility(View.INVISIBLE); // 隐藏固定范围的RoundNumber
                mCircle.setOrginView(holder.UnreadNumber);
            }

            @Override
            public void onMove(float curX, float curY) {
                mCircle.move(curX, curY);
            }
            @Override
            public void onUp() {
                mCircle.up();
            }
        });

    }
    @Override
    public int getItemCount() {
        return mPsychologistList.size();
    }
    public String getUser() {
        return user;
    }

}
